package cn.wolfcode.wms.service.impl;

import cn.wolfcode.wms.domain.Role;
import cn.wolfcode.wms.mapper.RoleMapper;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.IRoleService;
import cn.wolfcode.wms.util.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public void saveOrUpdate(Role entity,  Long[] permissIds,Long[] menuIds) {
        if (entity.getId() != null) {
            roleMapper.updateByPrimaryKey(entity);
            //删除关系,插入关系
            if (permissIds != null) {
                roleMapper.deleteRelation(entity.getId());
            }

            if (menuIds != null) {
                roleMapper.deleteRelationMenu(entity.getId());
            }
        } else {
            roleMapper.insert(entity);
        }
        if (permissIds != null) {
            for (Long permissId : permissIds) {

        roleMapper.insertRelation(entity.getId(),permissId);
            }
        }
        //菜单
        if (menuIds != null) {
            for (Long menuId : menuIds) {

                roleMapper.insertRelationMenu(entity.getId(),menuId);
            }
        }
    }

    @Override
    public void delete(Long id) {
        if (id != null) {
            roleMapper.deleteRelation(id);
            roleMapper.deleteRelationMenu(id);

            roleMapper.deleteByPrimaryKey(id);
        }
    }

    @Override
    public Role get(Long id) {

            Role entity = roleMapper.selectByPrimaryKey(id);
        System.out.println(66);
        return entity;

    }

    @Override
    public List<Role> selectall() {
        List<Role> depts = roleMapper.selectAll();
        return depts;
    }

    @Override
    public PageResult queryForList(QueryObject qo) {
        int rows = roleMapper.rows(qo);
        if (rows == 0) {
            return PageResult.EMPTY_RESULT;

        }
        PageResult result = new PageResult(qo.getCurrentPage(), qo.getPageSize(), rows, roleMapper.queryForList(qo));
        System.out.println(66);
        return result;

    }
}
